package cn.fl.institution.controller;

import cmm.comm.facade.livecheck.api.ICmmLiveChkApiService;
import cmm.comm.facade.livecheck.dto.CmmLiveChkDTO;
import cmm.mid.core.framework.dto.BaseParamDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.*;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.commons.result.ResponseData;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.preloan.dto.financialinterface.OpenAccountApplyRequest;
import cn.fl.preloan.dto.financialinterface.OpenAccountValidationRequest;
import cn.fl.preloan.enums.PreloanResultCodeEnum;
import cn.fl.preloan.financialinterface.IFinancialService;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjCstAccInfoService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjCstAccInfoVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 项目基础信息表 控制层 上实
 */
@Controller
@RequestMapping("/institution/ssfl")
public class PrjBscInfoInsSsflController {
    // 项目基础信息表
    @Autowired
    private IPrjBscInfoService prjBscInfoService;

    //项目客户基础信息表
    @Autowired
    private IPrjCstAccInfoService prjCstAccInfoService;

    // 自然人信息
    @Autowired
    private ICstNpBscInfoService cstNpBscInfoService;

    @Autowired
    private ICmmLiveChkApiService cmmLiveChkApiService;

    @Autowired
    private IFinancialService financialService;

    /**
     * 获取验证码
     */
    @RequestMapping(value = "/getVerificationCodeForSsfl.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO getVerificationCodeForSsfl(HttpServletRequest request) throws BaseException {
        BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码Start.qc:{}，操作人:{}", JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName()).info();
        //项目客户信息表id
        String cstAccInfoId = request.getParameter("cstAccInfoId");
        //机构code
        String insCode = request.getParameter("insCode");
        //项目id
        String prjId = request.getParameter("prjId");
        //客户id
        String cstId = request.getParameter("cstId");
        //客户姓名
        String cstNm = request.getParameter("cstNm");
        //银行账号
        String accNo = request.getParameter("accNo");
        //手机号
        String mp = request.getParameter("mp");
        //身份证号
        String certNo = request.getParameter("certNo");
        //银行代码
        String bkCd = request.getParameter("bkCd");
        //银行名称
        String bkNm = request.getParameter("bkNm");
        //签约有效截止日期
        String creditExpireDt = request.getParameter("creditExpireDt");
        //民族
        String nation = request.getParameter("nation");
        //证件有效起始日期
        String certValidSDt = request.getParameter("certValidSDt");
        //证件有效截止日期
        String certValidEDt = request.getParameter("certValidEDt");
        //证件签发机关
        String issuingOrg = request.getParameter("issuingOrg");
        //月收入
        String monthInc = request.getParameter("monthInc");
        //户籍地
        String domicileProvince = request.getParameter("domicileProvince");
        String domicileCity = request.getParameter("domicileCity");
        String idAddr = request.getParameter("idAddr");
        //居住地
        String postProvince = request.getParameter("postProvince");
        String postCity = request.getParameter("postCity");
        String postAddr = request.getParameter("postAddr");

        if (StringUtils.isEmpty(prjId)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目id为空!");
        }
        if (StringUtils.isEmpty(insCode)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "资管机构编码为空!");
        }
        if(StringUtils.isEmpty(certNo)){
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "身份证号为空!");
        }
        if (StringUtils.isEmpty(cstId)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户id为空!");
        }
        if (StringUtils.isEmpty(cstNm)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户名称为空!");
        }
        if (StringUtils.isEmpty(bkNm) || StringUtils.isEmpty(bkCd)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "开户银行为空!");
        }
        if (StringUtils.isEmpty(accNo)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行卡号!");
        }
        if (StringUtils.isEmpty(mp)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行预留手机号!");
        }
        if (StringUtils.isEmpty(creditExpireDt)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护签约有效截止日期!");
        }

        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "四要素校验通过");

        try {
            //保存自然人信息
            CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(Long.valueOf(cstId));
            if (null != cstNpBscInfoVO) {
                boolean flag = false;
                if (StringUtils.isNotEmpty(domicileProvince) && !Objects.equals(domicileProvince, cstNpBscInfoVO.getDomicileProvince())) {
                    cstNpBscInfoVO.setDomicileProvince(domicileProvince);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(domicileCity) && !Objects.equals(domicileCity, cstNpBscInfoVO.getDomicleCity())) {
                    cstNpBscInfoVO.setDomicleCity(domicileCity);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(idAddr) && !Objects.equals(idAddr, cstNpBscInfoVO.getIdAddr())) {
                    cstNpBscInfoVO.setIdAddr(idAddr);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(issuingOrg) && !Objects.equals(issuingOrg, cstNpBscInfoVO.getIssuingOrg())) {
                    cstNpBscInfoVO.setIssuingOrg(issuingOrg);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(postProvince) && !Objects.equals(postProvince, cstNpBscInfoVO.getPostProvince())) {
                    cstNpBscInfoVO.setPostProvince(postProvince);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(postCity) && !Objects.equals(postCity, cstNpBscInfoVO.getPostCity())) {
                    cstNpBscInfoVO.setPostCity(postCity);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(postAddr) && !Objects.equals(postAddr, cstNpBscInfoVO.getPostAddr())) {
                    cstNpBscInfoVO.setPostAddr(postAddr);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(nation) && !Objects.equals(nation, cstNpBscInfoVO.getNation())) {
                    cstNpBscInfoVO.setNation(nation);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(certValidSDt) && !Objects.equals(certValidSDt, cstNpBscInfoVO.getCertValidSDt())) {
                    cstNpBscInfoVO.setCertValidSDt(certValidSDt);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(certValidEDt) && !Objects.equals(certValidEDt, cstNpBscInfoVO.getCertValidEDt())) {
                    cstNpBscInfoVO.setCertValidEDt(certValidEDt);
                    flag = true;
                }
                if (StringUtils.isNotEmpty(monthInc)) {
                    Long mrIncMoney = DecimalUtils.convertYuanToFen(Double.valueOf(monthInc));
                    // 月收入转换年收入
                    Double yrInc = DecimalUtils.mul(mrIncMoney.doubleValue(), 12d);
                    if (!Objects.equals(cstNpBscInfoVO.getYrInc().doubleValue(), yrInc)) {
                        cstNpBscInfoVO.setYrInc(yrInc.longValue());
                        flag = true;
                    }
                }
                if (flag) {
                    cstNpBscInfoVO.setMdfTm(new Date());
                    cstNpBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
                    BaseLogUtils.newLogger("getVerificationCodeForSsfl.修改自然人信息.prjId:{},cstNpBscInfoVO:{}", prjId, JSON.toJSONString(cstNpBscInfoVO)).info();
                    this.cstNpBscInfoService.modify(cstNpBscInfoVO);
                }
            }
            // 四要素验证
            PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
            if (StringUtils.isNotEmpty(cstAccInfoId)) {
                condition.setId(Long.valueOf(cstAccInfoId));
            }
            PrjCstAccInfoQueryCondition prjCstAccInfo = new PrjCstAccInfoQueryCondition();
            prjCstAccInfo.setPrjId(Long.valueOf(prjId));
            prjCstAccInfo.setIsDel(BaseConstant.IS_YESNO_NO);
            prjCstAccInfo.setInsCode(insCode);
            List<PrjCstAccInfoVO> prjCstAccInfoVOS = this.prjCstAccInfoService.searchList(prjCstAccInfo);
            if (CollectionUtils.isNotEmpty(prjCstAccInfoVOS)) {
                PrjCstAccInfoVO prjCstAccInfoVO = prjCstAccInfoVOS.get(0);
                BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码.prjId:{},项目基本信息:{}", prjId, JSON.toJSONString(prjCstAccInfoVO)).info();
                condition.setId(prjCstAccInfoVO.getId());
                String oldcreditExpireDt = DateUtils.formatDateToString(prjCstAccInfoVO.getCreditExpireDt(), DateUtils.YYYY_MM_DD);
                if (!(cstNm.equals(prjCstAccInfoVO.getCstNm())
                        && certNo.equals(prjCstAccInfoVO.getCertNo())
                        && accNo.equals(prjCstAccInfoVO.getAccNo())
                        && mp.equals(prjCstAccInfoVO.getMp())
                        && creditExpireDt.equals(oldcreditExpireDt))) {
                    //四要素信息已修改，需要重新绑卡
                    if ("1".equals(prjCstAccInfoVO.getAccResCd())) {
                        condition.setAccResCd("0");
                    }
                } else if ("1".equals(prjCstAccInfoVO.getAccResCd())) {
                    //已绑卡成功且未修改四要素
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                    dataResultVO.setInfo("该银行卡绑卡已完成，请勿重新获取验证码！");
                    return dataResultVO;
                }
            }

            condition.setBkCd(bkCd);
            condition.setBkNm(bkNm);
            condition.setPrjId(Long.valueOf(prjId));
            condition.setCstId(Long.valueOf(cstId));
            condition.setInsCode(insCode);
            condition.setCstNm(cstNm);
            condition.setAccNo(accNo.trim());
            condition.setAccNm(cstNm);
            condition.setMp(mp);
            condition.setCertNo(certNo.trim());
            condition.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
            condition.setCreditExpireDt(DateUtils.formatStringToDate(creditExpireDt, DateUtils.YYYY_MM_DD));
            BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码.四要素校验Start.prjId:{},qc:{}", prjId, JSON.toJSONString(condition)).info();
            dataResultVO = this.prjCstAccInfoService.modifyPrjAccInfo(condition);
            // 判断四要素校验结果
            BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码.四要素校验End.prjId:{},result:{}", prjId, JSON.toJSONString(dataResultVO)).info();
            String chkRstCd = null, rstDesc = null;
            String bankCardType = null;
            if (dataResultVO != null && BaseConstant.IS_YESNO_YES == dataResultVO.getSuccess()) {
                Map<String, String> resultMap = (Map<String, String>) dataResultVO.getData();
                chkRstCd = resultMap.get("chkRstCd");
                rstDesc = resultMap.get("rstDesc");
                bankCardType = resultMap.get("bankCardType");
            }
            //四要素结果
            if (!BaseConstant.IS_YESNO_YES_STR.equals(chkRstCd)) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                dataResultVO.setInfo(rstDesc);
                return dataResultVO;
            }
            //银行卡类型
            if (!BaseConstant.IS_YESNO_NO_STR.equals(bankCardType)) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                dataResultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                return dataResultVO;
            }

            //获取验证码
            OpenAccountApplyRequest openAccountApplyRequest = new OpenAccountApplyRequest();

            //获取活体检测Id
            PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
            BaseParamDTO baseParamDTO = new BaseParamDTO();
            DataResultDTO dataResultDTO = this.cmmLiveChkApiService.getAllLiveChkByCstId(baseParamDTO, cstId, prjBscInfoVO.getLsCntNo());
            BaseLogUtils.newLogger("上实租赁获取活体检测id：prjId={}, dataDTO={}", prjId, JSON.toJSONString(dataResultDTO)).info();
            if (dataResultDTO != null && 1 == dataResultDTO.getSuccess()) {
                List<CmmLiveChkDTO> cmmLiveChkDTOList = (List<CmmLiveChkDTO>) dataResultDTO.getData();
                if (CollectionUtils.isNotEmpty(cmmLiveChkDTOList) && cmmLiveChkDTOList.get(0).getId() != null) {
                    openAccountApplyRequest.setCmmLiveChkId(cmmLiveChkDTOList.get(0).getId().toString());
                }
            }
            openAccountApplyRequest.setBizSN(prjId);
            openAccountApplyRequest.setInsCode(insCode);
            openAccountApplyRequest.setCustomerId(cstId);
            openAccountApplyRequest.setCustomerName(cstNm);
            openAccountApplyRequest.setIdCardNo(certNo);
            openAccountApplyRequest.setMobilePhone(mp);
            openAccountApplyRequest.setBankCardNo(accNo);
            openAccountApplyRequest.setSignExpireDate(DateUtils.formatDateToString(condition.getCreditExpireDt(), DateUtils.YYYYMMDD));
            BaseLogUtils.newLogger("上实租赁调用获取验证码接口Begin：prjId={}, request={}", prjId, JSON.toJSONString(openAccountApplyRequest)).info();
            ResponseData responseData = this.financialService.openAccountApply(openAccountApplyRequest);
            BaseLogUtils.newLogger("上实租赁调用获取验证码接口End:prjId={}, response={}", prjId, JSON.toJSONString(responseData)).info();
            if (PreloanResultCodeEnum.SUCCESS.getCode().equals(responseData.getCode())) {
                String openAccountStatus = (String) responseData.getData();
                if ("SUCCESS".equals(openAccountStatus) || "PROCESSING".equals(openAccountStatus)) {
                    dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                    dataResultVO.setInfo("验证码已发送至客户手机");
                    return dataResultVO;
                }
            }
            //发送验证码失败
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo("绑卡失败：" + responseData.getMessage());
        } catch (BaseException ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码失败,prjId:{},ERROR:{}", prjId, ex.getMessage()).info();
        } catch (Exception ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo("获取验证码,系统异常!");
            BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码失败,prjId:{},ERROR:{}", prjId, ex.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);
        }
        BaseLogUtils.newLogger("getVerificationCodeForSsfl.上实租赁获取验证码End.prjId:{},resultVO:{},操作人:{}", prjId, JSON.toJSONString(dataResultVO), CurrentThreadContext.getCurrentUserName()).info();
        return dataResultVO;
    }

    /**
     * 保存 项目客户信息
     */
    @RequestMapping(value = "/saveForSsfl.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO saveForSsfl(HttpServletRequest request) throws BaseException {
        BaseLogUtils.newLogger("saveForSsfl.保存项目客户信息Start.qc:{},usrNm:{}", JSON.toJSONString(request.getParameterMap()), CurrentThreadContext.getCurrentUserName()).info();
        //项目客户信息表id
        String cstAccInfoId = request.getParameter("cstAccInfoId");
        //机构code
        String insCode = request.getParameter("insCode");
        //项目id
        String prjId = request.getParameter("prjId");
        //客户id
        String cstId = request.getParameter("cstId");
        //客户姓名
        String cstNm = request.getParameter("cstNm");
        //银行账号
        String accNo = request.getParameter("accNo");
        //手机号
        String mp = request.getParameter("mp");
        //身份证号
        String certNo = request.getParameter("certNo");
        //银行代码
        String bkCd = request.getParameter("bkCd");
        //银行名称
        String bkNm = request.getParameter("bkNm");
        //签约有效截止日期
        String creditExpireDt = request.getParameter("creditExpireDt");
        //民族
        String nation = request.getParameter("nation");
        //证件有效起始日期
        String certValidSDt = request.getParameter("certValidSDt");
        //证件有效截止日期
        String certValidEDt = request.getParameter("certValidEDt");
        //证件签发机关
        String issuingOrg = request.getParameter("issuingOrg");
        //月收入
        String monthInc = request.getParameter("monthInc");
        //户籍地
        String domicileProvince = request.getParameter("domicileProvince");
        String domicileCity = request.getParameter("domicileCity");
        String idAddr = request.getParameter("idAddr");
        //居住地
        String postProvince = request.getParameter("postProvince");
        String postCity = request.getParameter("postCity");
        String postAddr = request.getParameter("postAddr");
        //短信验证码
        String phoneCode = request.getParameter("phoneCode");

        if (StringUtils.isEmpty(prjId)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目id为空!");
        }
        if (StringUtils.isEmpty(insCode)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "资管机构编码为空!");
        }
        if (StringUtils.isEmpty(cstId)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户id为空!");
        }
        if (StringUtils.isEmpty(cstNm)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户名称为空!");
        }
        if (StringUtils.isEmpty(bkNm) || StringUtils.isEmpty(bkCd)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护开户银行!");
        }
        if (StringUtils.isEmpty(accNo)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行卡号!");
        }
        if (StringUtils.isEmpty(mp)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护银行预留手机号!");
        }
        if (StringUtils.isEmpty(creditExpireDt)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护签约有效截止日期!");
        }
        if (StringUtils.isEmpty(nation)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护民族!");
        }
        if (StringUtils.isEmpty(issuingOrg)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护证件签发机关!");
        }
        if (StringUtils.isEmpty(monthInc)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护月收入!");
        }
        if (StringUtils.isEmpty(certValidSDt)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护证件有效起始日期!");
        }
        // 证件有效期(止)
        if (StringUtils.isEmpty(certValidEDt)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护证件有效终止日期!");
        } else {
            if ((!DateUtils.isDate(certValidEDt, "yyyy-MM-dd"))
                    && (!certValidEDt.equals(PropertiesCacheUtils.getProperty(SystemConstant.CHECK_DATE_LONG)))) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "请输入正确证件有效终止日期格式（yyyy-MM-dd 或 长期）!");
            }
        }
        if (StringUtils.isEmpty(domicileProvince)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护户籍地省!");
        }
        if (StringUtils.isEmpty(domicileCity)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护户籍地市!");
        }
        if (StringUtils.isEmpty(idAddr)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护户籍地详细地址!");
        }
        if (StringUtils.isEmpty(postProvince)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护居住地省!");
        }
        if (StringUtils.isEmpty(postCity)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护居住地市!");
        }
        if (StringUtils.isEmpty(postAddr)) {
            return new DataResultVO(BaseConstant.IS_YESNO_NO, "请维护居住地详细地址!");
        }

        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存成功");

        try {
            // 查询项目客户信息
            PrjCstAccInfoQueryCondition condition = new PrjCstAccInfoQueryCondition();
            if (StringUtils.isNotEmpty(cstAccInfoId)) {
                condition.setId(Long.valueOf(cstAccInfoId));
            }

            PrjCstAccInfoQueryCondition prjCstAccInfoQueryCondition = new PrjCstAccInfoQueryCondition();
            prjCstAccInfoQueryCondition.setPrjId(Long.valueOf(prjId));
            prjCstAccInfoQueryCondition.setIsDel(BaseConstant.IS_YESNO_NO);
            prjCstAccInfoQueryCondition.setInsCode(insCode);
            PrjCstAccInfoVO prjCstAccInfo = null;
            BaseLogUtils.newLogger("saveForSsfl.查询项目客户信息Start.prjId:{},qc:{}", prjId, JSON.toJSONString(prjCstAccInfoQueryCondition)).info();
            List<PrjCstAccInfoVO> prjCstAccInfoVOS = this.prjCstAccInfoService.searchList(prjCstAccInfoQueryCondition);
            BaseLogUtils.newLogger("saveForSsfl.查询项目客户信息End.prjId:{},list:{}", prjId, JSON.toJSONString(prjCstAccInfoVOS)).info();
            if (CollectionUtils.isNotEmpty(prjCstAccInfoVOS)) {
                prjCstAccInfo = prjCstAccInfoVOS.get(0);
                condition.setId(prjCstAccInfo.getId());
            }
            // 是否需要开户
            boolean isNeedSign = true;
            if (null != prjCstAccInfo && null != prjCstAccInfo.getId()) {
                if (StringUtils.isNotBlank(prjCstAccInfo.getAccResCd()) && "1".equals(prjCstAccInfo.getAccResCd())) {
                    isNeedSign = false;
                }
                String oldcreditExpireDt = DateUtils.formatDateToString(prjCstAccInfo.getCreditExpireDt(), DateUtils.YYYY_MM_DD);
                if (!(cstNm.equals(prjCstAccInfo.getCstNm())
                        && certNo.equals(prjCstAccInfo.getCertNo())
                        && accNo.equals(prjCstAccInfo.getAccNo())
                        && mp.equals(prjCstAccInfo.getMp())
                        && creditExpireDt.equals(oldcreditExpireDt))) {
                    //四要素信息已修改，需要重新获取验证码
                    return new DataResultVO(BaseConstant.IS_YESNO_NO, "四要素信息已修改，请重新获取短信验证码!");
                }
            }
            BaseLogUtils.newLogger("saveForSsfl.保存项目客户信息是否需要输入验证码.prjId:{},isNeedSign:{}", prjId, isNeedSign).info();
            if (isNeedSign) {
                // 短信验证码
                if (StringUtils.isBlank(phoneCode)) {
                    return new DataResultVO(BaseConstant.IS_YESNO_NO, "请输入短信验证码!");
                }
            }
            condition.setPrjId(Long.valueOf(prjId));
            condition.setCstId(Long.valueOf(cstId));
            condition.setInsCode(insCode);
            condition.setCstNm(cstNm);
            //保存自然人信息
            condition.setDomicileProvince(domicileProvince);
            condition.setDomicleCity(domicileCity);
            condition.setIdAddr(idAddr);
            condition.setLssueOffice(issuingOrg);
            condition.setPostProvince(postProvince);
            condition.setPostCity(postCity);
            condition.setPostAddr(postAddr);
            condition.setNation(nation);
            condition.setCertValidSDt(certValidSDt);
            condition.setCertValidEDt(certValidEDt);
            condition.setMonthInc(monthInc);
            boolean cstNpBscInfoResult = this.cstNpBscInfoService.modifyCstNpBscInfo(condition);
            BaseLogUtils.newLogger("saveForSsfl.修改自然人信息.prjId:{},condition:{},modify:{}", prjId, JSON.toJSONString(condition), cstNpBscInfoResult).info();
            // 保存项目客户信息，包含四要素验证
            condition.setBkCd(bkCd);
            condition.setBkNm(bkNm);
            condition.setAccNo(accNo.trim());
            condition.setAccNm(cstNm);
            condition.setMp(mp);
            condition.setCertNo(certNo.trim());
            condition.setCertTypCd(DictionaryConstant.CODE_CERT_TYP_CD_0);
            condition.setCreditExpireDt(DateUtils.formatStringToDate(creditExpireDt, DateUtils.YYYY_MM_DD));
            BaseLogUtils.newLogger("saveForSsfl.保存项目客户信息Start.prjId:{},condition:{}", prjId, JSON.toJSONString(condition)).info();
            dataResultVO = this.prjCstAccInfoService.modifyPrjAccInfo(condition);
            BaseLogUtils.newLogger("saveForSsfl.保存项目客户信息End.prjId:{},dataResultVO:{}", prjId, JSON.toJSONString(dataResultVO)).info();
            // 判断四要素校验结果
            String chkRstCd = null, rstDesc = null;
            String bankCardType = null;
            if (dataResultVO != null && BaseConstant.IS_YESNO_YES == dataResultVO.getSuccess()) {
                Map<String, String> resultMap = (Map<String, String>) dataResultVO.getData();
                chkRstCd = resultMap.get("chkRstCd");
                rstDesc = resultMap.get("rstDesc");
                bankCardType = resultMap.get("bankCardType");
            }
            //四要素结果
            if (!BaseConstant.IS_YESNO_YES_STR.equals(chkRstCd)) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                dataResultVO.setInfo(rstDesc);
                return dataResultVO;
            }
            //银行卡类型
            if (!BaseConstant.IS_YESNO_NO_STR.equals(bankCardType)) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                dataResultVO.setInfo("不支持此类银行卡，请更换为借记卡");
                return dataResultVO;
            }
            if (isNeedSign) {
                //卡类型和四要素校验成功，并且未开户，调用资方开户(绑卡)确认接口
                OpenAccountValidationRequest openAccountValidationRequest = new OpenAccountValidationRequest();
                openAccountValidationRequest.setBizSN(prjId);
                openAccountValidationRequest.setInsCode(insCode);
                openAccountValidationRequest.setCustomerId(cstId);
                openAccountValidationRequest.setCustomerName(cstNm);
                openAccountValidationRequest.setIdCardNo(certNo);
                openAccountValidationRequest.setMobilePhone(mp);
                openAccountValidationRequest.setBankCardNo(accNo);
                openAccountValidationRequest.setCheckCode(phoneCode);
                BaseLogUtils.newLogger("上实租赁调用开户接口Begin：prjId={}, request={}", prjId, JSON.toJSONString(openAccountValidationRequest)).info();
                ResponseData responseData = this.financialService.openAccountValidation(openAccountValidationRequest);
                BaseLogUtils.newLogger("上实租赁调用开户接口End：prjId={}, request={}", prjId, JSON.toJSONString(openAccountValidationRequest)).info();
                if (PreloanResultCodeEnum.SUCCESS.getCode().equals(responseData.getCode())) {
                    String openAccountStatus = (String) responseData.getData();
                    if ("SUCCESS".equals(openAccountStatus)) {
                        PrjCstAccInfoVO vo = new PrjCstAccInfoVO();
                        vo.setId(condition.getId());
                        //修改字段accResCd为1
                        vo.setAccResCd("1");
                        vo.setAccResMsg("绑卡成功");
                        BaseLogUtils.newLogger("上实租赁修改开户结果Start===prjId:{},vo:{}", prjId, JSON.toJSONString(vo)).info();
                        boolean modify = this.prjCstAccInfoService.modify(vo);
                        BaseLogUtils.newLogger("上实租赁修改开户结果End===prjId:{},modify:{}", prjId, modify).info();
                        if (!modify) {
                            dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "修改失败");
                        }
                        dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                        dataResultVO.setInfo("绑卡成功！");
                        return dataResultVO;
                    }
                }
                //开户失败
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
                dataResultVO.setInfo("绑卡短信验证失败：" + responseData.getMessage());
            } else {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                dataResultVO.setInfo("绑卡成功！");
            }
        } catch (BaseException ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("saveForSsfl.上实租赁保存项目客户信息失败.prjId:{},ERROR:{}", prjId, ex.getMessage()).info();
        } catch (Exception ex) {
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo("保存失败!");
            BaseLogUtils.newLogger("saveForSsfl.上实租赁保存项目客户信息失败,系统异常.prjId:{},ERROR:{}", prjId, ex.getMessage()).error(BaseExceptionConstant.INTERFACE_0102);
        }
        return dataResultVO;
    }
}
